package site.zhouinfo.servlet06;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhou
 * @create 2017-06-28 16:47
 * @email zhouinfo@qq.com
 * @blog blog.zhouinfo.site
 */
public class ServletSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 信息只保存到本次会话终止
        req.setAttribute("req","req");
        // 只要不关不换浏览器都可以取到这条信息
        req.getSession().setAttribute("getSession","getSession");
        // 只要不关闭tomcat都可以取到这条信息
        req.getServletContext().setAttribute("getServletContext","getServletContext");

        //跳转jsp 打印信息 req信息只能从这里取到
        req.getRequestDispatcher("/view/servlet06/ServletSession.jsp").forward(req,resp);
    }

    @Override
    public void init() throws ServletException {
        System.out.println("执行init方法");
        //web.xml <load-on-startup>1</load-on-startup>
        //可以不用执行doGet() 方法就有 访问jsp
        this.getServletContext().setAttribute("init","init_getServletContext");
    }
}
